package pers.lmd.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import pers.lmd.domain.entity.Note;
import pers.lmd.domain.vo.NoteStudentVO;
import pers.lmd.domain.vo.NoteTeacherVO;
import pers.lmd.mapper.NoteMapper;
import pers.lmd.service.INoteService;

import java.time.LocalDateTime;

/**
 * @author 10149
 * @description 针对表【note】的数据库操作Service实现
 * @createDate 2023-04-24 20:35:51
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class NoteServiceImpl extends ServiceImpl<NoteMapper, Note>
        implements INoteService {

    private final NoteMapper baseMapper;

    @Override
    public void addNote(LocalDateTime leaveTime, LocalDateTime backTime, String travel, String reason) {
        log.info(String.valueOf(leaveTime));
        Long id = StpUtil.getLoginIdAsLong();
        Note note = new Note();
        note.setLeaveTime(leaveTime);
        note.setBackTime(backTime);
        note.setTravel(travel);
        note.setReason(reason);
        note.setSid(id);
        note.setApplyStatus(1);
        save(note);

    }

    @Override
    public void updateNote(Long id, Integer applyStatus) {
        UpdateWrapper<Note> wrapper = new UpdateWrapper<>();
        wrapper
                .set("apply_status", applyStatus)
                .eq("id", id);
        baseMapper.update(null, wrapper);
    }

    @Override
    public Page<NoteStudentVO> getNoteStudentByPage(int pageNo, int pageSize) {
        Page<Note> page = Page.of(pageNo, pageSize);

        Long id = StpUtil.getLoginIdAsLong();
        LambdaQueryWrapper<Note> noteWrap = new LambdaQueryWrapper<>();

        noteWrap.
                select(Note::getLeaveTime, Note::getBackTime, Note::getTravel, Note::getReason, Note::getApplyStatus).
                eq(Note::getSid, id);

        page = baseMapper.selectPage(page,noteWrap);

        return (Page<NoteStudentVO>) page.convert(u->{
            NoteStudentVO v = new NoteStudentVO();
            BeanUtils.copyProperties(u, v);
            return v;
        });
    }

    @Override
    public Page<NoteTeacherVO> getNoteTeacherByPage(int pageNo, int pageSize) {
        Long id = StpUtil.getLoginIdAsLong();
        Page<NoteTeacherVO> page = Page.of(pageNo, pageSize);
        return baseMapper.getNoteByTid(page, id);
    }
}




